#!/usr/bin/perl
#save_recovery.cgi

require './diskman-lib_soft.pl';

&ReadParse();

my ($err_msg, $log_ev, $log_msg, $log_pri);

$err_msg="";
$log_ev="";

my $mkfs_lock;
$mkfs_lock = check_lock();
if ($mkfs_lock) {
	$err_msg = $text{"err_mkfslocked"};
	goto ERROR_OUT;
}

my ($volname, $adddisk);
$volname = $in{'volname'};
$adddisk = $in{'adddisk'};

if (!$volname || !$adddisk) {
	$err_msg = $text{"err_cgiparam"};
	goto ERROR_OUT;
}

my (@rdinfo, @dinfo, @free_disks);

&foreign_require("diskman","diskman-lib.pl");
@rdinfo = &foreign_call("diskman","get_check_raid_info");

foreach $r (@rdinfo){
	if ($r->{'volname'} eq $volname) {
		$rr = $r;
		last;
	}
}

$err_msg = $rr->{'volname'}.$rr->{'device'}.$adddisk;
#goto ERROR_OUT;
my $mddev = $rr->{'device'};
my $rdev = $dinfo[$n-1]->{'device'};

my $out;
$out = `lvm pvremove $addsisk -ff -y 2>/dev/null`;
if($?){
	$err_msg = $text{"err_cgiparam"};
	goto ERROR_OUT;
}
$out = `/sbin/mdadm $mddev --add $adddisk 2>&1`;
if ($?) {
	$log_ev = $text{'log_ev_recvolume_err'};
	$log_msg= &text('log_msg_recvolume_err', $volname);
	nas_syslog($text{'my_log_minor'}, 'naslog_warning', $log_ev, $log_msg);

	$err_msg = $text{"err_configchanged"};
	goto ERROR_OUT;	
}

$log_ev = $text{'log_ev_recvolume_ok'};
$log_msg= &text('log_msg_recvolume_ok', $volname);
nas_syslog($text{'my_log_minor'}, 'naslog_info', $log_ev, $log_msg);

#$rdev =~ /(\S+)(\d+)/;
#$out=`sfdisk --change-id $1 $2 fd`;
if ($?) {
	$log_ev = $text{'log_ev_changeid_err'};
	$log_msg= &text('log_msg_changeid_err', $rdev);
	nas_syslog($text{'my_log_minor'}, 'naslog_warning', $log_ev, $log_msg);
}

redirect("index.cgi");

ERROR_OUT:
&error_setup($text{'rec_err_title'});
&error($err_msg);
